package com.otod.sys.org.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.otod.base.exception.AppException;
import com.otod.commons.constants.CommonConstant;
import com.otod.commons.utils.Collections3;
import com.otod.base.service.BaseService;
import com.otod.sys.emp.mapper.EmpMapper;
import com.otod.sys.org.bo.OrgSaveBO;
import com.otod.sys.org.mapper.OrgMapper;
import com.otod.sys.org.service.IOrgService;
import com.otod.sys.role.service.IRoleService;
import com.otod.sys.vo.AuapTOrgVo;

@Service
public class OrgServiceImpl extends BaseService implements IOrgService {

	@Autowired
	IRoleService roleService;
	
	@Autowired
	OrgMapper orgMapper;
	
	@Autowired
	EmpMapper empMapper;

	@Override
	public void addOrg(OrgSaveBO orgSaveBO) throws Exception {
		AuapTOrgVo auapTOrgVo = new AuapTOrgVo();
		BeanUtils.copyProperties(orgSaveBO, auapTOrgVo);

		auapTOrgVo.setUuid(super.getUUID());
		auapTOrgVo.setIsDel(CommonConstant.COMMON_NO);// 设置删除标志,未删除
		orgMapper.insert(auapTOrgVo);
		auapTOrgVo.setOrgPath(calcOrgPath(orgSaveBO.getParentOrgId(),
				auapTOrgVo.getUuid()));// 设置orgPath
		
		Map<String, Object> map = new HashMap<String, Object>();
		if(true)throw new AppException("未完成代码");
		orgMapper.update(map);


		// 添加机构扩展信息
//		if (orgSaveBO.getArea() != null && !"".equals(orgSaveBO.getArea())) {
//			AuapTOrgExt auapTOrgExt = new AuapTOrgExt();
//			auapTOrgExt.setId(UUID.randomUUID().toString()
//					.replaceAll("-", ""));
//			auapTOrgExt.setOrgId(auapTOrgVo.getOrgId());
//			auapTOrgExt.setArea(orgSaveBO.getArea());
//			super.save(auapTOrgExt);
//		}
	}

	@Override
	public void eidtOrg(OrgSaveBO orgSaveBO) throws Exception {
		String newOrgPath = this.calcOrgPath(orgSaveBO.getParentOrgId(),
				orgSaveBO.getUuid());
		orgSaveBO.setOrgPath(newOrgPath);

		// 获取数据库中的老数据
		AuapTOrgVo old = orgMapper.getById(orgSaveBO.getUuid());
		String oldOrgPath = old.getOrgPath();

		// 组织层级改变，需要修改本身以及子组织的层级关系
		if (!oldOrgPath.equals(orgSaveBO.getOrgPath())) {
			// 获取该组织为根的子树
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("orgPath", oldOrgPath);
			List list = orgMapper.queryForList(param);
			// orgPath为新的
			int index = -1;
			String sub = "";
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				AuapTOrgVo bean = (AuapTOrgVo) iterator.next();
				// 将前面的旧父组织orgPath替换为新的orgPath
				index = bean.getOrgPath().indexOf(oldOrgPath);
				if (index == -1)
					continue;

				sub = bean.getOrgPath().substring(oldOrgPath.length());
				if (!sub.equals("")) {
					bean.setOrgPath(orgSaveBO.getOrgPath() + sub);
				} else {
					bean.setOrgPath(orgSaveBO.getOrgPath());
				}
				
				Map<String, Object> map = new HashMap<String, Object>();
				if(true)throw new AppException("未完成代码");
				orgMapper.update(map);
			}
		}
		BeanUtils.copyProperties(orgSaveBO, old);
		
		Map<String, Object> map = new HashMap<String, Object>();
		if(true)throw new AppException("未完成代码");
		orgMapper.update(map);

	}

	/**
	 * 计算orgPath 当前组织是顶级组织，orgPath=orgId，否则orgPath=上级组织.orgPath+","+orgId
	 * 
	 * @param parentOrgNo
	 *            父组织编号
	 * @param orgId
	 *            组织id
	 * @return
	 */
	private String calcOrgPath(String parentOrgNo, String orgId)
			throws Exception {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("orgNo", parentOrgNo);
		List list = orgMapper.queryForList(param);
		if (list != null && list.size() > 0) {
			AuapTOrgVo parent = (AuapTOrgVo) list.get(0);
			return parent.getOrgPath() + "," + orgId;
		} else {
			return orgId.toString();
		}
	}

	@Override
	public AuapTOrgVo getByOrgNo(String orgNo) throws Exception {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("orgNo", orgNo);
		List<AuapTOrgVo> list = orgMapper.queryForList(param);
		if(Collections3.isEmpty(list)){
			return null;
		}
		AuapTOrgVo auapTOrgVo = list.get(0);
		return auapTOrgVo;
	}



	@Override
	public void delOrg(String orgId) throws Exception {
		//判断该机构下是否存在子机构
		int count = orgMapper.getOrgChildrenCount(orgId);
		if(count > 0) {
			throw new Exception("机构下还有子机构，不能删除");
		}
		//判断该机构下是否存在用户
//		int count2 = empMapper.getEmpOrgCount(orgId);
//		if(count2 > 0) {
//			throw new Exception("机构下还有正常员工，不能删除");
//		}
		
		orgMapper.deleteById(orgId);
		
	}

}
